package netty.fry;

import net.dreamlu.iot.mqtt.core.client.MqttClient;
import org.apache.commons.text.StringSubstitutor;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhangtao
 * @create 2021/12/27 13:36
 */
public class PitcoFry {
    public static void main(String[] args) {
        String COOK_START = "{\n" +
                "    \"guid\": \"350e95bc6556d29c\",\n" +
                "    \"source\": \"fc:69:47:67:d8:b1\",\n" +
                "    \"destination\": \"0A:0B:0D:11:22:33\",\n" +
                "    \"dateTime\": \"2021-06-06T04:58:11+0000\",\n" +
                "    \"LQI\": 64,\n" +
                "    \"type\": \"COOK START\",\n" +
                "    \"recipeInstanceId\": ${recipeInstanceId},\n" +
                "    \"recipeName\": \"Prod 3          \",\n" +
                "    \"correlationId\": \"350e95bc6556d29c\"\n" +
                "}";

        String QUANTITY = "{\n" +
                "    \"guid\": \"76ce0a55d7da8ecd\",\n" +
                "    \"source\": \"fc:69:47:67:d8:b1\",\n" +
                "    \"destination\": \"0A:0B:0D:11:22:33\",\n" +
                "    \"dateTime\": \"2021-06-06T04:58:12+0000\",\n" +
                "    \"LQI\": 62,\n" +
                "    \"type\": \"COOK QUANTITY\",\n" +
                "    \"recipeInstanceId\": ${recipeInstanceId},\n" +
                "    \"recipeName\": \"Prod 3          \",\n" +
                "    \"quantity\": 4,\n" +
                "    \"correlationId\": \"350e95bc6556d29c\"\n" +
                "}";

        String TEMPERATURE = "{\n" +
                "    \"guid\": \"a751b9020958ab59\",\n" +
                "    \"source\": \"fc:69:47:67:d8:b1\",\n" +
                "    \"destination\": \"0A:0B:0D:11:22:33\",\n" +
                "    \"dateTime\": \"2021-06-06T04:58:29+0000\",\n" +
                "    \"LQI\": 61,\n" +
                "    \"type\": \"temperature\",\n" +
                "    \"temperature\": 3421,\n" +
                "    \"correlationId\": \"a751b9020958ab59\"\n" +
                "}";

        String COOK_DONE = "{\n" +
                "    \"guid\": \"1aaf5dcf7f9b6075\",\n" +
                "    \"source\": \"fc:69:47:67:d8:b1\",\n" +
                "    \"destination\": \"0A:0B:0D:11:22:33\",\n" +
                "    \"dateTime\": \"2021-06-06T05:03:00+0000\",\n" +
                "    \"LQI\": 58,\n" +
                "    \"type\": \"COOK COMPLETE\",\n" +
                "    \"recipeInstanceId\": ${recipeInstanceId},\n" +
                "    \"recipeName\": \"Prod 3          \",\n" +
                "    \"correlationId\": \"350e95bc6556d29c\"\n" +
                "}";

        // 初始化 mqtt 客户端
        MqttClient client = MqttClient.create()
                .ip("127.0.0.1")
                .port(1883)
                .connect();

        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(4);
        schedule.scheduleAtFixedRate(
                () -> {
                    Map<String, String> replaceMap = new HashMap<>();
                    replaceMap.put("recipeInstanceId", "0");
                    StringSubstitutor stringSubstitutor = new StringSubstitutor(replaceMap);
                    try {
                        client.publish("/us/qpm/server/workflow", ByteBuffer.wrap(stringSubstitutor.replace(COOK_START).getBytes()));
                        Thread.sleep(30000);
                        client.publish("/us/qpm/server/workflow", ByteBuffer.wrap(stringSubstitutor.replace(QUANTITY).getBytes()));
                        Thread.sleep(30000);
                        client.publish("/us/qpm/server/environmental", ByteBuffer.wrap(stringSubstitutor.replace(TEMPERATURE).getBytes()));
                        Thread.sleep(30000);
                        client.publish("/us/qpm/server/workflow", ByteBuffer.wrap(stringSubstitutor.replace(COOK_DONE).getBytes()));
                        Thread.sleep(30000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }, 5, 5 * 60, TimeUnit.SECONDS);
    }
}
